package com.hong.spring.config;

import com.hong.spring.listener.UserListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.StringUtils;
import org.zeromq.SocketType;
import org.zeromq.ZMQ;

/**
 * @author: csh
 * @Date: 2021/4/27 18:06
 * @Description:zero配置
 */
public class ZeroMqConsumer {

    @Autowired
    private UserListener userListener;

    private ZMQ.Socket push;
    private Integer threads;
    private SocketType zmqType;
    private String addr;

    public ZeroMqConsumer(Integer threads, SocketType zmqType, String addr) {
        this.threads = threads;
        this.zmqType = zmqType;
        this.addr = addr;
    }

    public void init(){
        ZMQ.Context context = ZMQ.context(threads);
        push = context.socket(zmqType);
        push.connect(addr);
        recvStr();
    }

    @Async
    public void recvStr(){
        while (true){
            String str = push.recvStr();
            if(!StringUtils.isEmpty(str)){
                userListener.Listener(str);
            }
        }
    }

}
